﻿<?php
class OrderController extends Controller{
	public function __construct($data) {
		parent::__construct($data);
	}

	public $goodslist;//{array {'id','goodname','goodprice','goodnum'} ,array {'goodid','goodname','goodprice','goodnum'}}
	public $extraprice;//array{'buyingprice'=>10,'sendprice'=>20,'tax'=>30}
	public $sendinfo;//array{'send_id'=>'001', 'orderpay'=>'orderpay1','ordersend'=>'ordersend1','sendcomment'->'sendcomment1'}
	public $cardinfo;//array('cardno'=>'aaaaaaaaaaaaa','carddate_year'=>'2013','carddate_month'=>'09');
	public $addressinfo;//array('address_id'=>'address_id',' sendname1'=>'sendname1','sendname2'=>'sendname2','sendnamef1'=>'sendnamef1','sendnamef2'=>'sendnamef2','zipcode'=>'zipcode1','localname'=>'localname1','treetname'=>'treetname1','address'=>'address1','telno'=>'999-999-9999','officename'=>'officename1','officenamef'=>'officenamef1');
	public $Registeraddressinfo;
	public $order_buylist;

	public $sum_goodsprice=0;

    public function temp()
    {
       
        $this->index(1);
    }
    
    public $taxtype;
    public $tax;
    public $taxTotal;
    public $Userid;
	public function index($istemp=0){

		try
		{
		 $this->_load_php_file("chklogin.php");
	        if(!isset($_SESSION['USER'])){
	    	
	       require_once( '../system/cookie.php' );
	    
	        }
            $this->loaduser();
            require_once( "../system/Class_Cart.php" );
            $strcart=Class_Cart::getCart($this->Userid);
            
			if(empty($strcart))
			{
				header('Location: pc_Top_index.html');return;
			}

			if(isset($this->Userid))
			{
				if($this->Userid!="")
				{
					$Login_Tag=true;
					$this->assign('Login_Tag',$Login_Tag);
                    
                        $temp;
                        $temp2;
                        if(!$istemp)
                        {
                            unset($_SESSION['shopjpaddressinfo']);
                            unset($_SESSION['shopjpRegisteraddressinfo']);
                        }
                        //echo "new";
                        $temp=$this->getuseraddress();
                        $temp2=$this->getregisteraddress_db();
                    

					$temp3=array(array(
							'sent_id'=>-1,
							'login_id'=>'',
							'sent_name'=>'会員登録住所',
							'sent_name1'=>$temp2['sendname1'],
							'sent_name2'=>$temp2['sendname2'],
							'sent_workshop_name1'=>$temp2['officename'],
							'sent_workshop_name2'=>$temp2['officenamef'],
							'sent_name_furikana1'=>$temp2['sendnamef1'],
							'sent_name_furikana2'=>$temp2['sendnamef2'],
							'sent_dep'=>'',
							'sent_zipcode'=>$temp2['zipcode'][0]."-".$temp2['zipcode'][1],
							'sent_local_cd'=>$temp2['local_cd'],
							'sent_treet_name'=>$temp2['treetname'],
							'sent_address'=>$temp2['address'],
							'sent_tel_number'=>$temp2['telno'][0]."-".$temp2['telno'][1]."-".$temp2['telno'][2],
							'c_time'=>'',
							'u_time'=>'',
							'c_user'=>'',
							'u_user'=>'',
							'local_name'=>$temp2['localname']

					));


					$temp3=array_merge($temp3,$temp);
					$this->assign('user_data',$temp3);

					// print_r($this->getuseraddress());

				}
				else
				{
					$this->assign('Login_Tag',false);

				}
			}
			else
			{
				$this->assign('Login_Tag',false);
			}
            
            $this->taxtype=$this->getTaxType();

            if($this->taxtype==1)
            {
                $this->taxTotal=$this->getTax();
                $this->tax=0;
            }
            else
            {
                $this->taxTotal=0;
                $this->tax=$this->getTax();
            }
           
			$this->getcart();

			$this->assign('order_buy',$this->getorderextraprice());

			//	 $this->assign('extraprice',$this->extraprice);

			$_SESSION['shopjpextraprice']=$this->extraprice;
			//
			$this->getcarddate();

			$this->getlocaldate();

			$this->setfomevalue();



			// print_r($this->extraprice);
			$this->_load_php_file("Class_FUNC.php");
			$this->_load_mdao_class("t_m_goods");
			$goods_dao = new t_m_goods;
			//
			$sql = "SELECT * FROM `d_goods` WHERE  goods_mark1='1' order by sellbegin desc LIMIT 0,10";
			$charts_data = $goods_dao->get_rows($sql);
			foreach ($charts_data as &$value){
				if(isset($value['goods_image']) && !empty($value['goods_image'])){
					$value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],1);
				}
               
			}
			if (isset($charts_data))
				$this->assign('charts_data',$charts_data);
			$this->assign('chartsing', $this->fetch('pc/charts.html'));
			$this->assign('pc_head', $this->fetch('pc/pc_head.html'));
			$this->assign('pc_foot', $this->fetch('pc/pc_foot.html'));
			$this->display('pc/order.html');return;

		}
		catch(Exception $e)
		{
			//	echo $e->getMessage();

			$this->assign('errmsg',$e->getMessage());

			$this->index();
			// $this->display('pc/order.html');

		}

	}



	public function ajaxupdateaddress()
	{
        $this->loaduser();
		//	parse_str($_SERVER["QUERY_STRING"]);

		//try

		//{
			$dsendid=isset($this->p["send_id"])?$this->p["send_id"]:"";
			$dname1=isset($this->p["name1"])?$this->p["name1"]:"";


			$dname2=isset($this->p["name2"])?trim($this->p["name2"]):"";
			$dzipcode=isset($this->p["zipcode"])?$this->p["zipcode"]:"";

			$dloca_cd=isset($this->p["loca_cd"])?$this->p["loca_cd"]:"";
			$dtreet_name=isset($this->p["treet_name"])?$this->p["treet_name"]:"";
			$dsent_address=isset($this->p["sent_address"])?$this->p["sent_address"]:"";
			$sent_tel_number=isset($this->p["sent_tel_number"])?$this->p["sent_tel_number"]:"";

			// $this->addressinfo=array();;


			echo $this->updateaddress($dname1,$dname2,$dzipcode,$dloca_cd,$dtreet_name,$dsent_address,$sent_tel_number,$dsendid);

		//}
		//catch(Exception $e)
		//{

		//	// throw  $e;// new Exception("card date error! ");
		//	$this->assign('errmsg',$e->getMessage());
		//	$this->index();

		//}

	}

	public function updateaddress($dname1,$dname2,$dzipcode,$dloca_cd,$dtreet_name,$dsent_address,$sent_tel_number,$dsendid)
	{


		try
		{

			$this->loaduser();

			$this->updateaddresscheck($dname1,$dname2,$dzipcode,$dloca_cd,$dtreet_name,$dsent_address,$sent_tel_number,$dsendid);
            $temp=array_filter($this->err_mes);
			if(count($temp)>0)
			{
                $str_js="$(\"[id=tiperr]\").remove();";
                foreach($this->err_mes as $key=>$value)
                {
                 
                    $str_js.="$(\"#Tip table tr:eq(".str_replace("tip_","",$key).") td:eq(1)\").append(\"<span style='color: red' id='tiperr' >".$value."</span>\");";
                   
                }
                echo $str_js;
				return;
			}
           
				require_once( "../system/Class_M_DAO.php" );

				try {


					$SQL="UPDATE  d_delivery set
					sent_name1='".$dname1."',
					sent_name2='".$dname2."',
					sent_zipcode='".$dzipcode."',
					sent_local_cd='".$dloca_cd."',
					sent_treet_name='".$dtreet_name."',
					sent_address='".$dsent_address."',
					sent_tel_number='".$sent_tel_number."',
					c_time =NOW(),
					u_time =NOW(),
					c_user ='".$this->Userid."',
					u_user ='".$this->Userid."'
					where sent_id='".$dsendid."' limit 1;";



					$M_dao = new Class_M_DAO;
					$M_dao->begin_trans();
					if(!$M_dao->exec_sql($SQL))
					{
						throw new Exception("alert('update delivery error');");
					}
					//	$newID = mysql_insert_id();

					$ret=mysql_affected_rows ();
					$M_dao->commit_trans();
                    //echo "alert(\"".$SQL."\");";
				}
				catch(Exception $e)
				{
					$M_dao->rollback_trans();
					throw $e;


				}
				return $ret;

		}
		catch(Exception $e)
		{

			echo $e->getMessage();;
			//	 $this->display('pc/order.html');

		}
	}





	public function loaduser()
	{

		if(isset($_SESSION['USER']['USER_id']))
		{
			$this->Userid=trim($_SESSION['USER']['USER_id']);
		}
		else
		{
			$this->Userid="";
		}

	}

	public function getcart()
	{
		try
		{

			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );

			$M_dao = new Class_M_DAO;
            
            require_once( "../system/Class_Cart.php" );
            $strcart=Class_Cart::getCart($this->Userid);
			if(!empty($strcart))
			{
                $arraycart=Class_Cart::cartStrToArray($strcart);
				$str_where=" 1=0 ";
				foreach ($arraycart as $Key=>$value)
				{
                    $str_where = $str_where." or id='". mysql_real_escape_string($Key)."' ";
				}

					$sql = "SELECT *,0 as goodnum FROM  d_goods  where ".$str_where;

					$good_data = $M_dao->get_rows($sql);

					$SumPrice=0;

					$index=0;
					//add by zuo
					$this->_load_php_file("Class_FUNC.php");
					foreach ($good_data as &$value){
						if(isset($value['goods_image']) && !empty($value['goods_image'])){
							$value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],3);
						}
                        if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){

                            $value['goods_unitprice'] = round($value['goods_unitprice']*(1+$this->tax)); 
                        }
					}
					//add by zuo end

					foreach ($good_data as &$value)
					{
						$value['goodnum']=$arraycart[(string)$value['id']];

						$good_data[$index]['goodnum']=$value['goodnum'];


						$SumPrice=$SumPrice+$value['goodnum']*$value['goods_unitprice'];

						//	sum_goodsprice=$SumPrice;
						if($value['goodnum']>0)
						{
							$goodslist[$index]=array('id'=>$value['id'],'goodsid'=>$value['goods_id'],'goodname'=>$value['goods_name'],'goodprice'=>$value['goods_unitprice'],'goodnum'=>$value['goodnum'],'goodimage'=>$value['goods_image']);
							$index=$index+1;




						}


					}

					//	print_r ($goodslist);

					if(!isset($goodslist))
					{
						header('Location: pc_Cart_index.html');
					}

					$_SESSION['shopjpgoodslist']=$goodslist;




					//	print_r ($good_data);

                   

					if (isset($goodslist))
					{

						$this->sum_goodsprice=$SumPrice;

						$TaxPrice=round($SumPrice*$this->taxTotal);

						$this->extraprice=array('SumPrice'=>$SumPrice,'buyingprice'=>0,'sendprice'=>0,'tax'=>$TaxPrice);

						//  $this->assign('SumPrice',$SumPrice);



					}
					$this->assign('good_data', $good_data);
                    $this->assign('taxtype', $this->taxtype);


			}

		}
		catch(Exception $e)
		{
			throw  $e;//	echo $e->getMessage();;
			//  $this->display('pc/order.html');

		}

	}

    public function getTaxType()
    {
    	require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );
            
            $M_dao = new Class_M_DAO;

			$sql3 = "SELECT flag_value FROM m_setflag WHERE flag_id=1";

			$order_tax = $M_dao->get_rows($sql3);

			return $order_tax[0]['flag_value'];
    }
	public function getTax()
	{
		try
		{
			$temptax=0;

			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );

			$M_dao = new Class_M_DAO;

			$sql3 = "SELECT flag_value FROM m_setflag WHERE flag_id=2";

			$order_tax = $M_dao->get_rows($sql3);

			//	print_r ($order_tax);

			$temptax=$order_tax[0]['flag_value'];

			return  $temptax/100;

			//  $this->assign('order_tax',$order_tax);
		}
		catch(Exception $e)
		{
			throw  $e;//	echo $e->getMessage();
			//	 $this->display('pc/order.html');

		}

	}

	public function getorderextraprice()
	{
		$card_Tag="0";
		try
		{

			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );

			$M_dao = new Class_M_DAO;

			$sql4 ="SELECT id,content,category,price,freeset,freeprice, comment FROM m_orderextraprice  where  category='0' ";

			$order_buy = $M_dao->get_rows($sql4);

			$sql5="SELECT id,content,category,price,freeset,freeprice,comment FROM m_orderextraprice  where  category='1' ";


			$order_fee=$M_dao->get_rows($sql5);

			$send_price=0;

			$index=0;

			foreach ($order_buy as $value1)
			{

				foreach ($order_fee as $value2)
				{

					if($value2['freeset']==1)
					{
						if($this->sum_goodsprice>=$value2['freeprice'])
						{
							$value2['price']="0";


						}


					}

					if($value1['freeset']==1)
					{
						if($this->sum_goodsprice>=$value1['freeprice'])
						{
							$value1['price']="0";

						}
					}

					$this->order_buylist[$index]=array(
			'id'=>$value2['id'].$value1['id'],
							'orderpayid'=>$value1['id'],

							'ordersendid'=>$value2['id'],

							'send_name'=>$value2['content']."+".$value1['content'],

							'orderpay'=>$value2['content'],

							'ordersend'=>$value1['content'],

							'payfreeset'=>$value2['freeset'],
							'buyingprice'=>$value2['price'],

							'payfreeprice'=>$value2['freeprice'],
							'paycomment'=>$value2['comment'],

							'sendprice'=>$value1['price'],
							'sendfreeset'=>$value1['freeset'],
							'sendfreeprice'=>$value1['freeprice'],
							'sendcomment'=>$value1['comment']);

					if($index==0)
					{
						$this->extraprice["buyingprice"]=$value2['price'];

						$this->extraprice["sendprice"]=$value1['price'];

						if($value2['id']=="3")
						{
							$card_Tag="1";

						}
						else
						{

							$card_Tag="0";

						}
					}

					$index=$index+1;
				}

			}
			$this->assign('card_Tag',$card_Tag);


			return $this->order_buylist;

		}
		catch(Exception $e)
		{
			throw $e;//	//	echo $e->getMessage();
			//	$this->display('pc/order.html');
		}
	}

	public function getuseraddress()
	{

		try
		{
			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );
			$M_dao = new Class_M_DAO;

			//$this->loaduser();

			$str_where2 ="c.id='".mysql_real_escape_string($this->Userid)."' ";

			$sql2="SELECT a.*,b.local_name  FROM d_delivery a left JOIN `m_local` b on a.`sent_local_cd`=b.`local_cd` left join m_user c on a.login_id=c.login_id where ".$str_where2;

			$user_data =$M_dao->get_rows($sql2);

			return $user_data;

		}
		catch(Exception $e)
		{
			throw $e;	//echo $e->getMessage();
			//	$this->display('pc/order.html');
		}

	}


	private function getregisteraddress_db()
	{

		try
		{
			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );
			$M_dao = new Class_M_DAO;

			//$this->loaduser();
            
			$str_where2 ="a.id='".mysql_real_escape_string($this->Userid)."'  limit 1 ";

			$sql2="SELECT a.*,b.local_name  FROM m_user a left JOIN m_local b on a.`local_cd`=b.`local_cd` where ".$str_where2;

			$register_data =$M_dao->get_rows($sql2);

			foreach($register_data as $value)
			{
				//$value[""]

				$registerAddrsee=array(
						'address_id'=>"",
						'address_name'=>"",
						'sendname1'=>$value['user_name1'],
						'sendname2'=>$value['user_name2'],
						'sendnamef1'=>$value['user_name_furikana1'],
						'sendnamef2'=>$value['user_name_furikana2'],
						'zipcode'=>explode("-",$value['zipcode']),
						'local_cd'=>$value['local_cd'],
						'localname'=>$value['local_name'],
						'treetname'=>$value['treet_name'],
						'address'=>$value['address'],
						'telno'=>explode("-",$value['tel_number']),
						'officename'=>$value['workshop_name'],
						'officenamef'=>$value['workshop_name_furikana'],
						'email'=>$value['email'],
						'email2'=>$value['email']
				);


			}

			return  $registerAddrsee;

			//	print_r($register_data);

			//  $this->assign('registerAddrsee',$registerAddrsee);

		}
		catch(Exception $e)
		{
			throw $e;	//echo $e->getMessage();
			//	$this->display('pc/order.html');
		}


	}


	public function getcarddate()
	{
		try

		{
			$t=time();
			$y=date('Y');

			//	echo 'Now:       '. date('Y') ."\n";
			// echo(date("Y",mktime($y+24*365)));

			$year=array();
			for($i=0;$i<25;$i++)
			{
				$year[]["year"]=trim(date("Y",mktime($y+24*365*$i)));

			}
			$this->assign('cardyear', $year);
			//print_r($year);
		}
		catch(Exception $e)
		{
			throw  $e;
			//	$this->display('pc/order.html');
		}

	}

	public function getlocaldate()
	{
		try
		{
			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );
			$M_dao = new Class_M_DAO;

			//	$this->loaduser();
			// $userID=$this->Userid;

			$sql2="SELECT local_cd,local_name  FROM m_local ";

			$local_data =$M_dao->get_rows($sql2);

			foreach($local_data as &$value)
			{
				$value['local_cd']=trim($value['local_cd']);

				$value['local_name']=trim($value['local_name']);
			}

			$this->assign('local_data', $local_data);

		}
		catch(Exception $e)
		{

			throw $e;
			//	echo $e->getMessage();
			//	$this->display('pc/order.html');
		}


	}


	public $err_mes;
	public $oerr;
	public function check()
	{
        $this->loaduser();
		//	parse_str($_SERVER["QUERY_STRING"]);
		require_once( '../system/ecjp_m.php' );
		require_once( '../system/Class_ERROR.php' );
		$this->oerr = new Class_ERROR('','');
		try
		{

			$cardinfo=array('cardstate'=>isset($this->p['h_card_state'])?$this->p['h_card_state']:"0",'cardno'=>isset($this->p['txtcard'])?$this->p['txtcard']:"",'carddate_year'=>isset($this->p['sel_year'])?$this->p['sel_year']:"",'carddate_month'=>isset($this->p['sel_month'])?$this->p['sel_month']:"");

			$_SESSION['shopjpcardinfo']=$cardinfo;


			$order_buylist=$this->getorderextraprice();



			// echo $this->p['payway']."----";

			if(isset($this->p['payway']))
			{
				$Lindex=$this->p['payway'];

				//$value['goodnum']=$cartinfo[(string)$value['id']];

				$index=0;

				$indextag=0;
				foreach ($order_buylist as $value)
				{

					if($order_buylist[$index]["id"]==$Lindex)
					{
						$indextag=$index;
					}
					$index=$index+1;

				}

				$this->sendinfo=array(

						'id'=>$order_buylist[$indextag]['id'],
						'ordersendid'=>$order_buylist[$indextag]['ordersendid'],
						'orderpayid'=>$order_buylist[$indextag]['orderpayid'],
						'send_name'=>$order_buylist[$indextag]['send_name'],

						'orderpay'=>$order_buylist[$indextag]['orderpay'],
		 'ordersend'=>$order_buylist[$indextag]['ordersend'],

						'buyingprice'=>$order_buylist[$indextag]['buyingprice'],
						'sendprice'=>$order_buylist[$indextag]['sendprice'],
						'sendcomment'=>$order_buylist[$indextag]['sendcomment'].$order_buylist[$indextag]['paycomment'],
				);



				if(isset($_SESSION['shopjpextraprice']))
				{
					$extraprice=$_SESSION['shopjpextraprice'];

					$extraprice["buyingprice"]=$order_buylist[$indextag]['buyingprice'];

					$extraprice["sendprice"]=$order_buylist[$indextag]['sendprice'];

					$_SESSION['shopjpextraprice']=$extraprice;

				}
			}

			$_SESSION['shopjpsendinfo']=$this->sendinfo;



			$this->getregisteraddress_form();
			$this->getaddress();



			$_SESSION['shopjpaddressinfo']=$this->addressinfo;



			$_SESSION['shopjpRegisteraddressinfo']=$this->Registeraddressinfo;

			// print_r($this->Registeraddressinfo);


			if(isset($cardinfo))
			{

				if($cardinfo["cardstate"]=="1")
				{
					$this->err_mes["msg_txtcard"]=$this->oerr->check($cardinfo["cardno"], array(CHK_EXIST));
					/*$cardinfoRet=$this->cardinfocheck($cardinfo);


					if($cardinfoRet!="")
					{
					// print_r($cardinfo);

					throw new Exception("card date error! ");

					}*/

					if($this->err_mes["msg_txtcard"]=="")
					{
						$carddate=$cardinfo["carddate_year"].$cardinfo["carddate_month"];

						require_once("../system/sbps/Class_sbps.php");

						$obj_sbps=new Class_sbps();
						$ret=$obj_sbps->trysend($cardinfo["cardno"],$carddate);
						if($ret!="")
						{

							$this->err_mes["msg_txtcard"]=$this->oerr->format_msg($ret);


							//$this->display('pc/order.html');
						}
					}
				}


			}

			$temp=array_filter($this->err_mes);
			if(count($temp)>0)
			{
               
				$this->assign('err_mes',$temp);
				$this->temp();
				return;
			}
			header('Location: pc_OrderCheck_index.html');
		}
		catch(Exception $e)
		{

			$this->assign('errmsg',$e->getMessage());
			//	header('Location: pc_Order_index.html');
			$this->temp();

		}

	}

	public function getregisteraddress_form()
	{

		//if(isset($_SESSION['USER']['USER_id'])&&)//
		//{

		//	$this->Registeraddressinfo=$this->getregisteraddress_db();

		//	// print_r($this->Registeraddressinfo);
		//}
		//else
		//{
			//非登陆时，手动输入
			$this->Registeraddressinfo=array(
					'address_id'=>"",
					'address_name'=>"-1",
					'sendname1'=>trim($this->p['rtxtsend_name1']),
					'sendname2'=>trim($this->p['rtxtsend_name2']),
					'sendnamef1'=>trim($this->p['rtxtsend_name_f1']),
					'sendnamef2'=>trim($this->p['rtxtsend_name_f2']),
					'zipcode'=>array(trim($this->p['rzipcode1']),trim($this->p['rzipcode2'])),
					'local_cd'=>trim($this->p['rselarea']),
					'localname'=>trim($this->p['h_r_local_name']),

					'treetname'=>trim($this->p['rtxtcity']),
					'address'=>trim($this->p['rtxtaddress']),
					'telno'=>array(trim($this->p['rtxttele0']),trim($this->p['rtxttele1']),trim($this->p['rtxttele2'])),
					//'telno2'=>$this->p['tele1'],
					// 'telno3'=>$this->p['tele2'],

					'officename'=>trim($this->p['rtxtcompany']),

					'officenamef'=>trim($this->p['rtxtcruby_k']),

					'email'=>trim($this->p['rtxtemail']),
					'email2'=>trim($this->p['rtxtemail2'])
			);

			$UserAddressRet=$this->registeraddressdatacheck($this->Registeraddressinfo);

			//if($UserAddressRet!="")
			//{

			//  throw new Exception("user address data error! ");

			//}
			//	print_r($this->Registeraddressinfo);



		//}

	}

	public function getaddress()
	{
		if(isset($_SESSION['USER']['USER_id']))//
		{
			//登录时
			if(isset($this->p['deliveryTo']) && $this->p['deliveryTo']!=0 )
			{
				//	print_r($this->p['deliveryTo']);
               
				//	print_r($this->getuseraddress());
                if($this->p['deliveryTo']==-1)
                {
                  
                    $this->addressinfo=$this->getregisteraddress_db();  
                }
				foreach ($this->getuseraddress() as $value)
				{

					if($value['sent_id']==$this->p['deliveryTo'])

					{

						$this->addressinfo=array(
								'address_id'=>$this->p['deliveryTo'],
								'address_name'=>$value['sent_name'],
								'sendname1'=>$value['sent_name1'],
								'sendname2'=>$value['sent_name2'],
								'sendnamef1'=>"",
								'sendnamef2'=>"",
								'zipcode'=>explode("-",$value['sent_zipcode']),
								'local_cd'=>$value['sent_local_cd'],
								'localname'=>$value['local_name'],
								'treetname'=>$value['sent_treet_name'],
								'address'=>$value['sent_address'],
								'telno'=>explode("-",$value['sent_tel_number']),
								'officename'=>$value['sent_workshop_name'],
								'officenamef'=>"");
						break;
					}
				}

				//$this->Registeraddressinfo=$this->getregisteraddress_db();
				// print_r($this->p['deliveryTo']);
			}

			else
			{

				$this->addressinfo=array(
						'address_id'=>"-1",
						'address_name'=>"",
						'sendname1'=>trim($this->p['txtsend_name1']),
						'sendname2'=>trim($this->p['txtsend_name2']),
						'sendnamef1'=>trim($this->p['txtsend_name_f1']),
						'sendnamef2'=>trim($this->p['txtsend_name_f2']),
						'zipcode'=>array(trim($this->p['zipcode1']),trim($this->p['zipcode2'])),
						'local_cd'=>trim($this->p['selarea']),
						'localname'=>trim($this->p['h_local_name']),

						'treetname'=>trim($this->p['txtcity']),
						'address'=>trim($this->p['txtaddress']),
						'telno'=>array(trim($this->p['txttele0']),trim($this->p['txttele1']),trim($this->p['txttele2'])),
						//'telno2'=>$this->p['tele1'],
						// 'telno3'=>$this->p['tele2'],

						'officename'=>trim($this->p['txtcompany']),

						'officenamef'=>trim($this->p['txtcruby_k']),
                        'newaddress'=>($this->p['chknewaddress']=='1'?trim($this->p['txtnewaddress']):"")
                        );

if($this->p['chknewaddress']=='1')
    $this->err_mes["msg_newaddress"]=$this->oerr->check($this->addressinfo["newaddress"], array(CHK_EXIST));
                
				$AddressRet=$this->addressdatacheck($this->addressinfo);



				if($AddressRet!="")
				{
					// throw new Exception($AddressRet);
					throw new Exception("address data error! ");

				}

				///////////////////////
				////////////////////注文地址
				//////////////////////
			}
		}
		else
		{
			//非登录时
			if(isset($this->p['ckb_register']))
			{
				$this->addressinfo=array(
						'address_id'=>"-1",
						'address_name'=>"-1",
						'sendname1'=>trim($this->p['txtsend_name1']),
						'sendname2'=>trim($this->p['txtsend_name2']),
						'sendnamef1'=>trim($this->p['txtsend_name_f1']),
						'sendnamef2'=>trim($this->p['txtsend_name_f2']),
						'zipcode'=>array(trim($this->p['zipcode1']),trim($this->p['zipcode2'])),
						'local_cd'=>trim($this->p['selarea']),
						'localname'=>trim($this->p['h_local_name']),

						'treetname'=>trim($this->p['txtcity']),
						'address'=>trim($this->p['txtaddress']),
						'telno'=>array(trim($this->p['txttele0']),trim($this->p['txttele1']),trim($this->p['txttele2'])),
						//'telno2'=>$this->p['tele1'],
						// 'telno3'=>$this->p['tele2'],

						'officename'=>trim($this->p['txtcompany']),

						'officenamef'=>trim($this->p['txtcruby_k']));



				$AddressRet=$this->addressdatacheck($this->addressinfo);



				if($AddressRet!="")
				{
					// throw new Exception($AddressRet);
					throw new Exception("address data error! ");

				}
			}
			else
			{
				//------------------------------
				$this->addressinfo=array(
						'address_id'=>"-1",
						'address_name'=>"",
						'sendname1'=>trim($this->p['rtxtsend_name1']),
						'sendname2'=>trim($this->p['rtxtsend_name2']),
						'sendnamef1'=>trim($this->p['rtxtsend_name_f1']),
						'sendnamef2'=>trim($this->p['rtxtsend_name_f2']),
						'zipcode'=>array(trim($this->p['rzipcode1']),trim($this->p['rzipcode2'])),
						'local_cd'=>trim($this->p['rselarea']),
						'localname'=>trim($this->p['h_r_local_name']),

						'treetname'=>trim($this->p['rtxtcity']),
						'address'=>trim($this->p['rtxtaddress']),
						'telno'=>array(trim($this->p['rtxttele0']),trim($this->p['rtxttele1']),trim($this->p['rtxttele2'])),
						//'telno2'=>$this->p['tele1'],
						// 'telno3'=>$this->p['tele2'],

						'officename'=>trim($this->p['rtxtcompany']),

						'officenamef'=>trim($this->p['rtxtcruby_k']),
				);

				//------------------------------------------
			}
		}


	}

	public function setfomevalue()
	{
		try
		{
			$emputcardinfo=array('cardstate'=>"",'cardno'=>"",'carddate_year'=>"",'carddate_month'=>"");

			$cardinfo=isset($_SESSION['shopjpcardinfo'])?$_SESSION['shopjpcardinfo']:$emputcardinfo;

			$emputaddressinfo=array(

					'address_id'=>"",
					'address_name'=>"",
					'sendname1'=>"",
					'sendname2'=>"",
					'sendnamef1'=>"",
					'sendnamef2'=>"",
					'zipcode'=>array("",""),
					// 'zipcode2'=>"",
					'local_cd'=>"",
					'localname'=>"",
					'treetname'=>"",
					'address'=>"",
					'telno'=>array("","",""),
					// 'telno2'=>"",
					// 'telno3'=>"",
					'officename'=>"",
					'officenamef'=>"",
					'email'=>"",
					'email2'=>""

			);

			$this->addressinfo=isset($_SESSION['shopjpaddressinfo'])?$_SESSION['shopjpaddressinfo']:$emputaddressinfo;




			$this->Registeraddressinfo=isset($_SESSION['shopjpRegisteraddressinfo'])?$_SESSION['shopjpRegisteraddressinfo']:$emputaddressinfo;


			// print_r($this->Registeraddressinfo);

			if($this->Registeraddressinfo["address_name"]!="-1")
			{

				if($this->Userid!="")
				{

					$this->Registeraddressinfo=$this->getregisteraddress_db();
				}

			}

			//;


			// print_r($this->Registeraddressinfo);

			$emputsendinfo=array(
					'id'=>"",
					'ordersendid'=>"",
					'orderpayid'=>"",
					'send_name'=>"",

					'orderpay'=>"",
					'ordersend'=>"",
					'buyingprice'=>"",
					'sendprice'=>"",
					'sendcomment'=>""
			);

			$this->sendinfo=isset($_SESSION['shopjpsendinfo'])?$_SESSION['shopjpsendinfo']:$emputsendinfo;

			$emputextraprice=array(
					'SumPrice'=>"",
					'buyingprice'=>"",
					'sendprice'=>"",
					'tax'=>""

			);

			$this->extraprice=isset($_SESSION['shopjpextraprice'])?$_SESSION['shopjpextraprice']:$emputextraprice;



			$this->assign('extraprice',$this->extraprice);

			$this->assign('cardinfo',$cardinfo);

			$this->assign('addressinfo',$this->addressinfo);

			$this->assign('registerAddrsee',$this->Registeraddressinfo);

			$this->assign('sendinfo',$this->sendinfo);

		}

		catch(Exception $e)
		{

			throw $e;
			///	echo $e->getMessage();;
			//	 $this->display('pc/order.html');

		}

	}
	public function datacheck($data_v,$em,$length,$name)
	{

		if(isset($data_v) && !empty($data_v))
		{

			$str_len=((strlen($data_v) +mb_strlen($data_v,'UTF8'))/2);



			if($str_len>$length)
			{

				throw new Exception("stringlength limit the length of the entry !"); //"stringlength limit the length of the entry !


			}

			if(!$em && trim($data_v)=="")
			{
				throw new Exception("cant not intput space character ! ");

			}


		}
		elseif(!$em)
		{

			//	print_r($data_v);
			//	$mse=$name."can not be empty !";

			throw new Exception("can not be empty !");

			//	return 3;

		}
	}


	public function updateaddresscheck($dname1,$dname2,$dzipcode,$dloca_cd,$dtreet_name,$dsent_address,$sent_tel_number,$dsendid)
	{
        require_once( '../system/ecjp_m.php' );
		require_once( '../system/Class_ERROR.php' );
		$this->oerr = new Class_ERROR('','');
			//$this->datacheck($dname1,false,50,"氏");
			//$this->datacheck($dname2,false,100,"名");

			//$result=$this->datacheck($dzipcode,false,8,"郵便番号");
			//$result=$this->datacheck($dloca_cd,false,3,"都道CD");

			//$result=$this->datacheck($dtreet_name,false,100,"市区町村");
			//$result=$this->datacheck($dsent_address,false,100,"以降の住所");
			//$result=$this->datacheck($sent_tel_number,false,20,"電話番号");


        $temp1=$this->oerr->check($dname1, array(CHK_EXIST)); 
        $temp2=$this->oerr->check($dname2, array(CHK_EXIST));

            if($temp1.$temp2!="")
                $this->err_mes["tip_0"]=($temp1==""?$temp2:$temp1);
          

				$zipcode=$dzipcode;

				//郵便番号
                $this->err_mes["tip_1"]=$this->oerr->check(str_replace("-","",$zipcode), array(CHK_EXIST));
                if($this->err_mes["tip_1"]=="")
                {$this->err_mes["tip_1"]=$this->oerr->check($zipcode, array(CHK_ZIP));}

				//都道CD
				$this->err_mes["tip_2"]=$this->oerr->check($dloca_cd, array(CHK_EXIST));
				//都道府県
				//$this->err_mes["tip_localname"]=$this->oerr->check($this->addressinfo["localname"], array(CHK_EXIST));
				//市区町村
				$this->err_mes["tip_3"]=$this->oerr->check($dtreet_name, array(CHK_EXIST));
				//以降の住所
				$this->err_mes["tip_4"]=$this->oerr->check($dsent_address, array(CHK_EXIST));

				$telno=$sent_tel_number;
				//電話番号
                $this->err_mes["tip_5"]=$this->oerr->check(str_replace("-","",$telno), array(CHK_EXIST));
                if($this->err_mes["tip_5"]=="")
                    $this->err_mes["tip_5"]=$this->oerr->check($telno, array(CHK_TEL));


				//$this->datacheck($this->addressinfo["officename"],true,200,"貴医院名,貴技工所名");
				//$this->datacheck($this->addressinfo["officenamef"],true,200,"貴医院名,貴技工所名のフリガナ"); //

                
			return  "";//$result;

		

	}

	public function addressdatacheck()
	{



		try
		{
			if(isset($this->addressinfo))
			{

				//$this->datacheck($this->addressinfo["sendname1"],false,50,"氏");
				$this->err_mes["msg_sendname1"]=$this->oerr->check($this->addressinfo["sendname1"], array(CHK_EXIST));
				//$this->datacheck($this->addressinfo["sendname2"],false,100,"名");
				$this->err_mes["msg_sendname2"]=$this->oerr->check($this->addressinfo["sendname2"], array(CHK_EXIST));

				if($this->err_mes["msg_sendname1"].$this->err_mes["msg_sendname2"]!="")
					$this->err_mes["msg_sendname"]=($this->err_mes["msg_sendname1"]==""?$this->err_mes["msg_sendname2"]:$this->err_mes["msg_sendname1"]);

				//$this->datacheck($this->addressinfo["sendnamef1"],false,50,"氏のフリガナ");
				$this->err_mes["msg_sendnamef1"]=$this->oerr->check($this->addressinfo["sendnamef1"], array(CHK_EXIST));
				//$this->datacheck($this->addressinfo["sendnamef2"],false,50,"名のフリガナ");
				$this->err_mes["msg_sendnamef2"]=$this->oerr->check($this->addressinfo["sendnamef2"], array(CHK_EXIST));

				if($this->err_mes["msg_sendnamef1"].$this->err_mes["msg_sendnamef2"]!="")
					$this->err_mes["msg_sendnamef"]=($this->err_mes["msg_sendnamef1"]==""?$this->err_mes["msg_sendnamef2"]:$this->err_mes["msg_sendnamef1"]);


				$zipcode=$this->addressinfo["zipcode"][0]."-".$this->addressinfo["zipcode"][1];

				//$this->datacheck($zipcode,false,8,"郵便番号");
                $this->err_mes["msg_zipcode"]=$this->oerr->check($this->addressinfo["zipcode"][0].$this->addressinfo["zipcode"][1], array(CHK_EXIST));
                 if($this->err_mes["msg_zipcode"]=="")
                 {$this->err_mes["msg_zipcode"]=$this->oerr->check($zipcode, array(CHK_ZIP));}

				//$this->datacheck($this->addressinfo["local_cd"],false,3,"都道CD");
				$this->err_mes["msg_local_cd"]=$this->oerr->check($this->addressinfo["local_cd"], array(CHK_EXIST));
				//$this->datacheck($this->addressinfo["localname"],false,30,"都道府県");
				$this->err_mes["msg_localname"]=$this->oerr->check($this->addressinfo["localname"], array(CHK_EXIST));
				//$this->datacheck($this->addressinfo["treetname"],false,100,"市区町村");
				$this->err_mes["msg_treetname"]=$this->oerr->check($this->addressinfo["treetname"], array(CHK_EXIST));
				//$this->datacheck($this->addressinfo["address"],false,100,"以降の住所");
				$this->err_mes["msg_address"]=$this->oerr->check($this->addressinfo["address"], array(CHK_EXIST));

				$telno=$this->addressinfo["telno"][0]."-".$this->addressinfo["telno"][1]."-".$this->addressinfo["telno"][2];
				//$this->datacheck($telno,false,20,"電話番号");
                $this->err_mes["msg_telno"]=$this->oerr->check($this->addressinfo["telno"][0].$this->addressinfo["telno"][1].$this->addressinfo["telno"][2], array(CHK_EXIST));
                if($this->err_mes["msg_telno"]=="")
				$this->err_mes["msg_telno"]=$this->oerr->check($telno, array(CHK_TEL));


				//$this->datacheck($this->addressinfo["officename"],true,200,"貴医院名,貴技工所名");
				//$this->datacheck($this->addressinfo["officenamef"],true,200,"貴医院名,貴技工所名のフリガナ"); //

				return "";

			}
			else
			{
				//$this->err_mes["msg_send_name1"]=$this->oerr->check("", array(CHK_EXIST));

				throw new Exception("data empty! ");

			}

		}
		catch(exception $e)
		{

			return  $e->getMessage();
			//	return false;

			//	echo $e->getMessage();

			//	$this->display('pc/order.html');

		}

	}

	public function registeraddressdatacheck()
	{



		try
		{
			if(isset($this->Registeraddressinfo))
			{

				// $this->datacheck($this->addressinfo["address_id"],false,11,"配送Id");
				// $this->datacheck($this->addressinfo["address_name"],false,100,"配送先名");
				/*$this->datacheck($this->addressinfo["sendname1"],false,50,"氏");
				$this->datacheck($this->addressinfo["sendname2"],false,100,"名");
				$this->datacheck($this->addressinfo["sendnamef1"],false,50,"氏のフリガナ");
				$this->datacheck($this->addressinfo["sendnamef2"],false,50,"名のフリガナ");



				$zipcode=$this->addressinfo["zipcode"][0]."-".$this->addressinfo["zipcode"][1];

				$this->datacheck($zipcode,false,8,"郵便番号");




				$this->datacheck($this->addressinfo["local_cd"],false,3,"都道CD");
				$this->datacheck($this->addressinfo["localname"],false,30,"都道府県");
				$this->datacheck($this->addressinfo["treetname"],false,100,"市区町村");
				$this->datacheck($this->addressinfo["address"],false,100,"以降の住所");

				$telno=$this->addressinfo["telno"][0]."-".$this->addressinfo["telno"][1]."-".$this->addressinfo["telno"][2];

				$this->datacheck($telno,false,20,"電話番号");
				$this->datacheck($this->addressinfo["officename"],true,200,"貴医院名,貴技工所名");
				$this->datacheck($this->addressinfo["officenamef"],true,200,"貴医院名,貴技工所名のフリガナ"); //

				return "";
				 */

				$this->err_mes["msg_rsendname1"]=$this->oerr->check($this->Registeraddressinfo["sendname1"], array(CHK_EXIST));

				$this->err_mes["msg_rsendname2"]=$this->oerr->check($this->Registeraddressinfo["sendname2"], array(CHK_EXIST));
				if($this->err_mes["msg_rsendname1"].$this->err_mes["msg_rsendname2"]!="")
					$this->err_mes["msg_rsendname"]=($this->err_mes["msg_rsendname1"]==""?$this->err_mes["msg_rsendname2"]:$this->err_mes["msg_rsendname1"]);

				$this->err_mes["msg_rsendnamef1"]=$this->oerr->check($this->Registeraddressinfo["sendnamef1"], array(CHK_EXIST));

				$this->err_mes["msg_rsendnamef2"]=$this->oerr->check($this->Registeraddressinfo["sendnamef2"], array(CHK_EXIST));
				if($this->err_mes["msg_rsendnamef1"].$this->err_mes["msg_rsendnamef2"]!="")
					$this->err_mes["msg_rsendnamef"]=($this->err_mes["msg_rsendnamef1"]==""?$this->err_mes["msg_rsendnamef2"]:$this->err_mes["msg_rsendnamef1"]);



				$zipcode=$this->Registeraddressinfo["zipcode"][0]."-".$this->Registeraddressinfo["zipcode"][1];

				//$this->datacheck($zipcode,false,8,"郵便番号");

				$this->err_mes["msg_rzipcode"]=$this->oerr->check($zipcode, array(CHK_ZIP));

				$this->err_mes["msg_rlocal_cd"]=$this->oerr->check($this->Registeraddressinfo["local_cd"], array(CHK_EXIST));

				$this->err_mes["msg_rlocalname"]=$this->oerr->check($this->Registeraddressinfo["localname"], array(CHK_EXIST));

				$this->err_mes["msg_rtreetname"]=$this->oerr->check($this->Registeraddressinfo["treetname"], array(CHK_EXIST));

				$this->err_mes["msg_raddress"]=$this->oerr->check($this->Registeraddressinfo["address"], array(CHK_EXIST));

				$telno=$this->Registeraddressinfo["telno"][0]."-".$this->Registeraddressinfo["telno"][1]."-".$this->Registeraddressinfo["telno"][2];

				$this->err_mes["msg_rtelno"]=$this->oerr->check($telno, array(CHK_TEL));

				$this->err_mes["msg_remail"]=$this->oerr->check($this->Registeraddressinfo["email"], array(CHK_EXIST));

				if($this->err_mes["msg_remail"]=="")
				{
					$this->err_mes["msg_remail"]=$this->oerr->check($this->Registeraddressinfo["email"], array(CHK_EMAIL));

				}

				//$this->err_mes["msg_remail2"]=$this->oerr->check($this->Registeraddressinfo["email2"], array(CHK_EMAIL));

				if($this->Registeraddressinfo["email"]!=$this->Registeraddressinfo["email2"])
				{
					$this->err_mes["msg_remail2"]=$this->oerr->format_msg('メールアドレスを同じにしてください');
				}
			}
			else
			{
				throw new Exception("data empty! ");
			}
		}
		catch(exception $e)
		{

			return  $e->getMessage();
			//	return false;

			//	echo $e->getMessage();

			//	$this->display('pc/order.html');

		}

	}

	public function cardinfocheck($cardinfo)
	{


		try
		{
			if(isset($cardinfo))
			{

				if($cardinfo["cardstate"]=="1")
				{
					$this->datacheck($cardinfo["cardno"],false,20,"");
					$this->datacheck($cardinfo["carddate_year"],false,4,"");
					$this->datacheck($cardinfo["carddate_month"],false,2,"");

				}

				return "";

			}
			else
			{

				// return false;
				throw new Exception("date error! ");

			}

		}
		catch(exception $e)
		{

			//	return false;

			return $e->getMessage();

			//	$this->display('pc/order.html');

		}


	}

}